//
//  NKBabyController.m
//  NiKi
//
//  Created by qiang chen on 16/6/11.
//  Copyright © 2016年 qiang chen. All rights reserved.
//

#import "NKListenController.h"
#import <SDCycleScrollView.h>
#import "NKHomeCell.h"
#import "NKHomeCollectionHeadView.h"
#import "NKAgeCategroyPopView.h"
#import "UIButton+OxTitleAndImageType.h"
#import "NKListenCategroyController.h"
#import "NKListeningController.h"
#import "NKMoiveInfoController.h"

#define kCyclePicVHeight  135.f/667 * ScreenH
#define kCellH (ScreenW - 15)/3.f
#define kHistoryViewH 190.f

@interface NKListenController ()<SDCycleScrollViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,NKAgeCategroyPopViewDelegate>{
    
    __weak IBOutlet UICollectionView *_collectionV;
    IBOutlet UIView *_historyV;
    __weak IBOutlet UIButton *_backTopBtn;
    UIButton *_selectedCateBtn;
    
    __weak IBOutlet UIButton *_ageCateBtn;
    __weak IBOutlet UIButton *_cateBtn;
    
}

@property (nonatomic, strong) SDCycleScrollView *cyclePicV;
@property (nonatomic, strong) NKAgeCategroyPopView *ageCategroyV;
@property (nonatomic, strong) NKListenCategroyController *listenCategroyController;
@end

@implementation NKListenController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

#pragma mark - 设置UI
- (SDCycleScrollView *)cyclePicV {
    if(!_cyclePicV) {
        _cyclePicV = [SDCycleScrollView cycleScrollViewWithFrame:(CGRect){{0,-kCyclePicVHeight- kHistoryViewH},ScreenW,kCyclePicVHeight} delegate:self placeholderImage:nil];//缺一张默认图片
        self.cyclePicV.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
    }
    return _cyclePicV;
}

- (NKAgeCategroyPopView *)ageCategroyV {
    if (!_ageCategroyV) {
        _ageCategroyV = [[NSBundle mainBundle]loadNibNamed:NSStringFromClass([NKAgeCategroyPopView class]) owner:nil options:nil].firstObject;
        _ageCategroyV.delegate = self;
    }
    return _ageCategroyV;
}

- (NKListenCategroyController *)listenCategroyController {
    if (!_listenCategroyController) {
        _listenCategroyController = [[NKListenCategroyController alloc]init];
        [self addChildViewController:_listenCategroyController];
    }
    return _listenCategroyController;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat y = scrollView.contentOffset.y;
    if (y < -kCyclePicVHeight-kHistoryViewH) {
        self.cyclePicV.y = y;
        self.cyclePicV.height = fabs(y)-kHistoryViewH;
    }
    
    _backTopBtn.hidden = !(y > kBackTopRadio*ScreenH);
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (void)setupUI {
    _collectionV.contentInset =  UIEdgeInsetsMake(kCyclePicVHeight+kHistoryViewH, 0, 0, 0);
    
    [_collectionV registerNib:[UINib nibWithNibName:NSStringFromClass([NKHomeCell class]) bundle:nil] forCellWithReuseIdentifier:homeCell];
    [_collectionV registerNib:[UINib nibWithNibName:NSStringFromClass([NKHomeCollectionHeadView class]) bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:homeCollectionHeadView];
    
    
    self.cyclePicV.imageURLStringsGroup = @[
                                            @"http://pic.nipic.com/2008-01-09/20081982231131_2.jpg",
                                            @"http://pic.58pic.com/58pic/16/45/46/31m58PICqtn_1024.jpg",
                                            @"http://image31.bookschina.com/2011/zuo/5037320tp2.jpg"
                                            ];
    [_collectionV addSubview:self.cyclePicV];
    
    _historyV.frame = CGRectMake(0, -190, ScreenW, 190);
    [_collectionV addSubview:_historyV];
    
    [_cateBtn layoutButtonWithEdgeInsetsStyle:OxButtonEdgeInsetsRight space:100.f/210*(ScreenW-110)];
}

#pragma mark - More
- (IBAction)moreHistoryBtnClick:(UIButton *)sender {
}


#pragma mark - CateBtn
- (IBAction)categroyBtnClick:(UIButton *)sender {
    self.listenCategroyController.view.frame = self.view.bounds;
    [self.view addSubview:self.listenCategroyController.view];
    [UIView transitionFromView:self.view toView:self.listenCategroyController.view duration:0.5 options:UIViewAnimationOptionTransitionFlipFromTop completion:^(BOOL finished) {
        
    }];

}

- (IBAction)ageCategroyBtnClick:(UIButton *)sender {
    self.ageCategroyV.frame = CGRectMake(0, 0, ScreenW, ScreenH);
    self.ageCategroyV.btnBgV.transform = CGAffineTransformMakeScale(0.1, 0.1);
    self.ageCategroyV.blurV.hidden = YES;
    [self.view addSubview:self.ageCategroyV];
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:10 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.ageCategroyV.btnBgV.transform = CGAffineTransformMakeScale(1.0, 1.0);
        self.ageCategroyV.blurV.hidden = NO;
    } completion:^(BOOL finished) {
        
    }];
}

- (IBAction)backTopBtnClick:(UIButton *)sender {
    [_collectionV setContentOffset:CGPointMake(0, -kCyclePicVHeight-kHistoryViewH) animated:YES];
}


#pragma mark - CollectionVDelegaet
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 4;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 20;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NKHomeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homeCell forIndexPath:indexPath];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(kCellH, kCellH);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(ScreenW, 44);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (kind == UICollectionElementKindSectionHeader) {
        NKHomeCollectionHeadView *headView = (NKHomeCollectionHeadView *)[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:homeCollectionHeadView forIndexPath:indexPath];
        return headView;
    }
    return nil;
}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.type == ControllerType_Listen) {
        [self.navigationController pushViewController:[NKListeningController new] animated:YES];
    }else{
        [self.navigationController pushViewController:[NKMoiveInfoController new] animated:YES];
    }
    
}

#pragma mark - ageBtnDelegate
- (void)ageCategroyPopView:(NKAgeCategroyPopView *)popView ageBtnClick:(UIButton *)ageBtn {
    [_ageCateBtn setTitle:ageBtn.titleLabel.text forState:UIControlStateNormal];
}

@end
